<template>
  <el-dialog :title="currentPid?'编辑部门':'新增部门'" :visible="showDialog" @close="close">
    <el-form
      ref="deptForm"
      :model="deptForm"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" size="mini" style="width: 80%" />
      </el-form-item>
      <el-form-item label="部门类型" prop="type">
        <el-select v-model="deptForm.type" size="mini">
          <el-option label="分公司" value="分公司" />
          <el-option label="部门" value="部门" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerName">
        <el-select v-model="deptForm.managerName" size="mini">
          <el-option label="张三三" value="张三三" />
          <el-option label="李四四" value="李四四" />
          <el-option label="何笑笑" value="何笑笑" />
          <el-option label="李维" value="李维" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门描述" prop="introduce">
        <el-input
          v-model="deptForm.introduce"
          type="textarea"
          style="width: 80%"
          :rows="2"
        />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="16">
            <el-button
              size="mini"
              type="primary"
              @click="submit"
            >确定</el-button>
            <el-button size="mini">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartments, obtainDepa, ChangeDepartment } from '@/api/user'

export default {
  name: 'Adddept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentPid: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      deptForm: {
        name: '',
        type: '',
        managerName: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 8, message: '部门名称是1-8位字符', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '部门类型不能为空', trigger: 'blur' }
        ],
        managerName: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    close() {
      this.$emit('close')
      this.$refs.deptForm.resetFields()
      this.deptForm = {
        name: '',
        type: '',
        managerName: '',
        introduce: ''
      }
    },
    async  submit() {
      const value = await this.$refs.deptForm.validate()
      if (value) {
        if (this.deptForm.id) {
          await ChangeDepartment({ ...this.deptForm })
          this.$message.success('修改部门成功')
        } else {
          const res = await addDepartments({ ...this.deptForm, pid: this.currentPid })
          this.$message.success('新增部门成功')
          console.log(res, 'bbbbbbbbbbbb')
        }
        this.close()
      }
    },
    // 编辑
    async getDepartmentDetail() {
    // 调用获取部门详情接口
      const res = await obtainDepa(this.currentPid)
      this.deptForm = res.data
    }

  }

}

</script>

<style>
</style>
